package cn.com.captureNetPacket.iatConfigImpl;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.PreDestroy;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import cn.com.captureNetPacket.iatConfig.IatConfig;
import cn.com.captureNetPacket.model.SignallingInfo;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
@ConditionalOnProperty(name = "cnp.iat.enable", havingValue = "true",matchIfMissing = false)
public class IatTestConfig implements IatConfig {
		@Value("${cnp.iat.connectUrl}")
		private   String iflytekConnectString;
		private static int lockCount=0;
		 
//		public static void initIatConfig(String iflytekConnect){
//			iflytekConnectString=iflytekConnect;
//		}
		
		
		 
		
		
			/**
			* <strong>Title: <strong>
			* <p>Description: <p>
			* <p>Company: </p> 
			*<strong>Copyright:</strong> Copyright (c) 2019
			* @version: 1.0
			* @author  ASUS:augustWei
			* @date  2021 下午3:58:38
			* @param rate
			* @return
			* @see cn.com.captureNetPacket.iatConfig.IatConfig#iatOnlineBegin(int)
			*/
		@Override
		public String iatOnlineBegin(int rate,SignallingInfo signallingInfo) {
			 
			String ssbparam = "svc=iat,auf=audio/L8;rate="+8000+",aue=alaw,type=1,uid=660Y5r,appid=pc20onli,url=" + iflytekConnectString;
			String session_id=signallingInfo.getDeviceNumber();
			try {
			log.debug("iatOnlineBegin  初始化参数  : {}",ssbparam) ;
			   
			
			}catch (Exception e) {
				log.error("iatOnlineBegin session_id: {}",session_id);
			}
			 
			log.debug("beginSession  : {} 在线识别会话已开始。",session_id);
			  return session_id;
		}
		
		
		
			/**
			* <strong>Title: <strong>
			* <p>Description: <p>
			* <p>Company: </p> 
			*<strong>Copyright:</strong> Copyright (c) 2019
			* @version: 1.0
			* @author  ASUS:augustWei
			* @date  2021 下午3:58:38
			* @param session_id
			* @param buffer
			* @param audioStatus
			* @return
			* @see cn.com.captureNetPacket.iatConfig.IatConfig#iatOnlineWrite(java.lang.String, byte[], int)
			*/
		@Override
		public Map<String, Object> iatOnlineWrite(String session_id,byte[] buffer,int audioStatus,SignallingInfo signallingInfo) {
	         Map<String, Object> product=null;
           product=new HashMap<String, Object>();
           product.put("result", "session_id: "+session_id);
       		log.info("iatOnlineWrite ，session_id: {} ",session_id);
			return product;
		}
		
		
		
		
		
			/**
			* <strong>Title: <strong>
			* <p>Description: <p>
			* <p>Company: </p> 
			*<strong>Copyright:</strong> Copyright (c) 2019
			* @version: 1.0
			* @author  ASUS:augustWei
			* @date  2021 下午3:58:38
			* @param ssbparam
			* @return
			* @see cn.com.captureNetPacket.iatConfig.IatConfig#iatOffline(java.lang.String)
			*/
		@Override
		public String iatOffline(String ssbparam,SignallingInfo signallingInfo) {
			//离线识别
			ssbparam = "svc=iat,auf=audio/L16;rate=8000,aue=raw,type=1,uid=660Y5r,appid=pc20onli,url=" + iflytekConnectString +"," +"extend_params={\"params\":\"seginfo=1,vspp=1,online=off\"}";
			return null;
		}
			/**
			* <strong>Title: <strong>
			* <p>Description: <p>
			* <p>Company: </p> 
			*<strong>Copyright:</strong> Copyright (c) 2019
			* @version: 1.0
			* @author  ASUS:augustWei
			* @date  2021 下午3:58:38
			* @param ssbparam
			* @return
			* @see cn.com.captureNetPacket.iatConfig.IatConfig#iatTranslate(java.lang.String)
			*/
		@Override
		public String iatTranslate(String ssbparam,SignallingInfo signallingInfo) {
			//识别后翻译
			ssbparam = "url=" + iflytekConnectString + ",svc=src,appid=pc20onli,scenes=iat;itr,scenes_params={\"iat\":\"svc=iat,auf=audio/L16;rate=16000,aue=raw,type=1,uid=660Y5r\"|\"itr\":\"type=cnug\"}";

			return null;
		}
		
			/**
			* <strong>Title: <strong>
			* <p>Description: <p>
			* <p>Company: </p> 
			*<strong>Copyright:</strong> Copyright (c) 2019
			* @version: 1.0
			* @author  ASUS:augustWei
			* @date  2021 下午3:58:38
			* @param session_id
			* @see cn.com.captureNetPacket.iatConfig.IatConfig#endSession(java.lang.String)
			*/
		@Override
		public  void endSession(String session_id,SignallingInfo signallingInfo) {
			// 结束一路会话
			log.debug("endSession  : {} 会话已结束。",session_id);
			if(lockCount<1)
				destory();
			
		}
		
		 
			/**
			* <strong>Title: <strong>
			* <p>Description: <p>
			* <p>Company: </p> 
			*<strong>Copyright:</strong> Copyright (c) 2019
			* @version: 1.0
			* @author  ASUS:augustWei
			* @date  2021 下午3:58:38
			* @see cn.com.captureNetPacket.iatConfig.IatConfig#destory()
			*/
		@Override
		@PreDestroy
		public  void destory() {
			log.debug("IatTestConfig destory 正在结束iat资源");
		}


			@Override
			public void initIatConfig() {
				log.debug("initIatConfig ----------->>>>>>>>>>>iflytekConnectString: {} ",iflytekConnectString);
				 
			}
 
   }
